﻿using LiveCharts.Wpf;
using LiveCharts;
using Ping9719.WpfEx.Mvvm;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json;
using System.IO;
using HandyControl.Tools.Extension;
using LiveCharts.SeriesAlgorithms;

namespace BaseUi.ViewModels
{
    public class BarChartViewModelNew : BindableBase
    {
        private List<string> labels;
        public List<string> Labels
        {
            get { return labels; }
            set { labels = value; }
        }
        //本地保存路径
        public static readonly string DailyStatisticsPath = System.IO.Path.Combine(LocalFile.DataDir, "outputStat");
        //文件锁
        static object lockObject = new object();
        private DateTime StartTime_ = DateTime.Now.Date;
        /// <summary>
        /// 开始时间
        /// </summary>
        public DateTime StartTime
        {
            get => StartTime_;
            set
            {
                StartTime_ = value;
            }
        }
        public SeriesCollection SeriesCollection { get; set; }
        public ChartValues<int> Valuesok = new ChartValues<int> { 0, 0, 0, 0, 0 };
        public ChartValues<int> Valuesng = new ChartValues<int> { 0, 0, 0, 0, 0 };
        public Func<double, string> Format { get; set; }
        public BarChartViewModelNew()
        {

            Labels = new List<string>()
            {
                "总产量",
                "1号测试位",
                "2号测试位",
                "3号测试位",
                "4号测试位",
            };

            SeriesCollection = new SeriesCollection
            {
                new ColumnSeries
                {
                    Title = "数量",
                    Values = Valuesok,
                    Width=10,DataLabels=true,

                }
                //,
                //new ColumnSeries
                //{
                //    Title = "ng",
                //    Values = Valuesng,
                //    Width=10,DataLabels=true,
                //},
            };
            Format = value => value.ToString("N");
        }
        //public void updata()
        //{
        //    try
        //    {
        //        Valuesok[1] = ;
        //        Valuesok[2] = ;
        //        Valuesok[3] = ;
        //        Valuesok[4] = ;
        //        Valuesok[0] = ;

        //        Valuesng[1] = ;
        //        Valuesng[2] = ;
        //        Valuesng[3] = ;
        //        Valuesng[4] = ;
        //        Valuesng[0] = Valuesng[1] + Valuesng[2] + Valuesng[3] + Valuesng[4];
        //    }
        //    catch (Exception ee)
        //    {
        //        Logs.Write(ee);
        //        Valuesok[0] = -1;
        //        Valuesok[1] = -1;
        //        Valuesok[2] = -1;
        //        Valuesok[3] = -1;
        //        Valuesok[4] = -1;

        //        Valuesng[0] = -1;
        //        Valuesng[1] = -1;
        //        Valuesng[2] = -1;
        //        Valuesng[3] = -1;
        //        Valuesng[4] = -1;
        //    }
        //}

        //public void UpdataData()
        //{
        //    lock (lockObject)
        //    {

        //        try
        //        {

        //            Valuesok[1] = DataDb.dbBox.Queryable<BoxInfo>().Where(o => o.Num == "1号测试位" && o.IsOk == "合格" && o.Time > DateTime.Today).Count();
        //            Valuesok[2] = DataDb.dbBox.Queryable<BoxInfo>().Where(o => o.Num == "2号测试位" && o.IsOk == "合格" && o.Time > DateTime.Today).Count();
        //            Valuesok[3] = DataDb.dbBox.Queryable<BoxInfo>().Where(o => o.Num == "3号测试位" && o.IsOk == "合格" && o.Time > DateTime.Today).Count();
        //            Valuesok[4] = DataDb.dbBox.Queryable<BoxInfo>().Where(o => o.Num == "4号测试位" && o.IsOk == "合格" && o.Time > DateTime.Today).Count();
        //            Valuesok[0] = Valuesok[1] + Valuesok[2] + Valuesok[3] + Valuesok[4];

        //            Valuesng[1] = DataDb.dbBox.Queryable<BoxInfo>().Where(o => o.Num == "1号测试位" && o.IsOk != "合格" && o.Time > DateTime.Today).Count();
        //            Valuesng[2] = DataDb.dbBox.Queryable<BoxInfo>().Where(o => o.Num == "2号测试位" && o.IsOk != "合格" && o.Time > DateTime.Today).Count();
        //            Valuesng[3] = DataDb.dbBox.Queryable<BoxInfo>().Where(o => o.Num == "3号测试位" && o.IsOk != "合格" && o.Time > DateTime.Today).Count();
        //            Valuesng[4] = DataDb.dbBox.Queryable<BoxInfo>().Where(o => o.Num == "4号测试位" && o.IsOk != "合格" && o.Time > DateTime.Today).Count();
        //            Valuesng[0] = Valuesng[1] + Valuesng[2] + Valuesng[3] + Valuesng[4];
        //        }

        //        catch (Exception ee)
        //        {
        //            Logs.Write(ee);
        //            Valuesok[0] = -1;
        //            Valuesok[1] = -1;
        //            Valuesok[2] = -1;
        //            Valuesok[3] = -1;
        //            Valuesok[4] = -1;

        //            Valuesng[0] = -1;
        //            Valuesng[1] = -1;
        //            Valuesng[2] = -1;
        //            Valuesng[3] = -1;
        //            Valuesng[4] = -1;
        //        }
        //    }
        //}
        public void UpdataData()
        {
            lock (lockObject)
            {

                try
                {

                    Valuesok[1] = DataDb.dbBox.Queryable<BoxInfo>().Where(o => o.Num == "1号测试位" && o.Time > DateTime.Today).Count();
                    Valuesok[2] = DataDb.dbBox.Queryable<BoxInfo>().Where(o => o.Num == "2号测试位" && o.Time > DateTime.Today).Count();
                    Valuesok[3] = DataDb.dbBox.Queryable<BoxInfo>().Where(o => o.Num == "3号测试位" && o.Time > DateTime.Today).Count();
                    Valuesok[4] = DataDb.dbBox.Queryable<BoxInfo>().Where(o => o.Num == "4号测试位" && o.Time > DateTime.Today).Count();
                    Valuesok[0] = Valuesok[1] + Valuesok[2] + Valuesok[3] + Valuesok[4];


                }

                catch (Exception ee)
                {
                    Logs.Write(ee);
                    Valuesok[0] = -1;
                    Valuesok[1] = -1;
                    Valuesok[2] = -1;
                    Valuesok[3] = -1;
                    Valuesok[4] = -1;


                }
            }
        }


    }
}


//public void updata()
//{
//    try
//    {
//        Valuesok[1] = DataDb.dbBox.Queryable<BoxInfo>().Where(o => o.Num == "1号测试位" && o.IsOk == "合格" && o.Time > DateTime.Today).Count();
//        Valuesok[2] = DataDb.db.Queryable<BoxInfo>().Where(o => o.Num == "2号测试位" && o.IsOk == "合格" && o.Time > DateTime.Today).Count();
//        Valuesok[3] = DataDb.db.Queryable<BoxInfo>().Where(o => o.Num == "3号测试位" && o.IsOk == "合格" && o.Time > DateTime.Today).Count();
//        Valuesok[4] = DataDb.db.Queryable<BoxInfo>().Where(o => o.Num == "4号测试位" && o.IsOk == "合格" && o.Time > DateTime.Today).Count();
//        Valuesok[0] = Valuesok[1] + Valuesok[2] + Valuesok[3] + Valuesok[4];

//        Valuesng[1] = DataDb.db.Queryable<BoxInfo>().Where(o => o.Num == "1号测试位" && o.IsOk != "合格" && o.Time > DateTime.Today).Count();
//        Valuesng[2] = DataDb.db.Queryable<BoxInfo>().Where(o => o.Num == "2号测试位" && o.IsOk != "合格" && o.Time > DateTime.Today).Count();
//        Valuesng[3] = DataDb.db.Queryable<BoxInfo>().Where(o => o.Num == "3号测试位" && o.IsOk != "合格" && o.Time > DateTime.Today).Count();
//        Valuesng[4] = DataDb.db.Queryable<BoxInfo>().Where(o => o.Num == "4号测试位" && o.IsOk != "合格" && o.Time > DateTime.Today).Count();
//        Valuesng[0] = Valuesng[1] + Valuesng[2] + Valuesng[3] + Valuesng[4];
//    }
//    catch (Exception ee)
//    {
//        Logs.Write(ee);
//        Valuesok[0] = -1;
//        Valuesok[1] = -1;
//        Valuesok[2] = -1;
//        Valuesok[3] = -1;
//        Valuesok[4] = -1;

//        Valuesng[0] = -1;
//        Valuesng[1] = -1;
//        Valuesng[2] = -1;
//        Valuesng[3] = -1;
//        Valuesng[4] = -1;
//    }
//}
